package com.rockcent.service.impl;

import com.rockcent.helper.MerchantFundFlowrHelper;
import com.rockcent.mall.lib.domain.MallFund;
import com.rockcent.mall.lib.domain.MerchantDeductionsFlow;
import com.rockcent.mall.lib.domain.MerchantFund;
import com.rockcent.mall.lib.domain.enumclass.*;
import com.rockcent.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by zhangjh on 2017/4/5.
 */
@Service
public class MerchantServiceFeeSrvImpl implements MerchantServiceFeeSrv {

    @Autowired
    private MerchantFundSrv merchantFundSrv;
    @Autowired
    private MallFundSrv mallFundSrv;

    @Autowired
    private MallFundFlowSrv mallFundFlowSrv;
    @Autowired
    private MerchantFundFlowSrv merchantFundFlowSrv;

    @Autowired
    private MerchantDeductionsFlowSrv merchantDeductionsFlowSrv;

    @Override
    public void deductions(Long mallId, Long merchantId, String settlementTime, BigDecimal operationAmount) {


        //商户扣费
        MerchantFund merchantFund = merchantFundSrv.findOne(merchantId);
        BigDecimal totalAmount = merchantFund.getTotalAmount();
        BigDecimal presentRechargeAmount = merchantFund.getPresentRechargeAmount();
        BigDecimal actualRechargeAmount = merchantFund.getActualRechargeAmount();
        BigDecimal availableAmount = merchantFund.getAvailableAmount();
        BigDecimal canNotWithdrawAmount = merchantFund.getCanNotWithdrawAmount();

        MallFund mallFund = mallFundSrv.findOne(mallId);

        MerchantDeductionsFlow merchantDeductionsFlow = merchantDeductionsFlowSrv.findTop1ByMallIdAndMerchantIdAndSettlementDateAndDeductionsStatusAndDeductionsType(mallId, merchantId, settlementTime, MerchantDeductionsStatus.SUCCESS, MerchantDeductionsType.BASE_SERVICE_PAYMENTS);

        if (null == merchantDeductionsFlow) {
            String seqNo = MerchantFundFlowrHelper.SINGLETON.createMerchantFundSeqNo(MerchantDeductionsType.BASE_SERVICE_PAYMENTS);
            merchantDeductionsFlow = new MerchantDeductionsFlow();
            merchantDeductionsFlow.setMallId(mallId);
            merchantDeductionsFlow.setMerchantId(merchantId);
            merchantDeductionsFlow.setSeqNo(seqNo);
            merchantDeductionsFlow.setDeductionsType(MerchantDeductionsType.BASE_SERVICE_PAYMENTS);
            merchantDeductionsFlow.setSettlementDate(settlementTime);
            merchantDeductionsFlow.setOperationAmount(operationAmount);
            //TODO 不够扣？
            if (availableAmount.compareTo(operationAmount) < 0) {
                merchantDeductionsFlow.setDeductionsStatus(MerchantDeductionsStatus.FAIL);
                merchantDeductionsFlow.setRemark("可用金额不够扣");
            } else {//够扣

                //平台加钱
                mallFund.setAvailableAmount(mallFund.getAvailableAmount().add(operationAmount));
                mallFund.setTotalAmount(totalAmount.add(operationAmount));

                //商户扣钱
                merchantFund.setAvailableAmount(merchantFund.getAvailableAmount().subtract(operationAmount));
                merchantFund.setTotalAmount(merchantFund.getTotalAmount().subtract(operationAmount));

                BigDecimal ZERO = new BigDecimal(0l);
                BigDecimal remainSubtractAmount = canNotWithdrawAmount.subtract(operationAmount);
                if (remainSubtractAmount.compareTo(ZERO) > 0) {
                    merchantFund.setCanNotWithdrawAmount(remainSubtractAmount);
                } else {
                    merchantFund.setCanNotWithdrawAmount(ZERO);
                }

                //扣费流水
                mallFundSrv.save(mallFund);
                merchantFundSrv.save(merchantFund);

//            rechargeFlow.setSeqNo(seqNo);

                //商户资金流水表
                merchantFundFlowSrv.save(merchantFund, operationAmount, MerchantPayType.BASE_SERVICE_PAYMENTS, seqNo, FundTransactionFlow.EXPEND, MerchantFundFlowTransactionType.A_MERCHANT_SERVICE_FEE_EXPEND,"商户扣费");

                //平台资金流水表
                mallFundFlowSrv.save(mallFund, operationAmount, PaymentMethod.BASE_SERVICE_PAYMENTS, seqNo, FundTransactionFlow.EXPEND, MallFundFlowTransactionType.MERCHANT_SERVICE_FEE_INCOME);


                merchantDeductionsFlow.setDeductionsStatus(MerchantDeductionsStatus.SUCCESS);

                //TODO 商户状态启用？
            }
            merchantDeductionsFlowSrv.save(merchantDeductionsFlow);
        }


    }
}
